<template>
    <div class="system-sysDatasource-container">
        <el-dialog title="新增数据源" v-model="isShowDialog" >
            <el-form ref="addDatasourceFormRef" :model="formData" :rules="formRules" label-width="120px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据源名称" prop="databaseName">
                            <el-input v-model="formData.databaseName" placeholder="请输入数据源名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据库类型" prop="databaseType">
                            <el-select v-model="formData.databaseType" placeholder="请选择数据库类型" clearable class="w100">
								<el-option label="MYSQL" value="MYSQL"></el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据源类型" prop="datasourceType">
                            <el-select v-model="formData.datasourceType" placeholder="请选择数据源类型" clearable class="w100">
								<el-option label="com.zaxxer.hikari.HikariDataSource" value="com.zaxxer.hikari.HikariDataSource"></el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据源驱动类名称" prop="driverClassName">
                            <el-select v-model="formData.driverClassName" placeholder="请选择数据源驱动类名称" clearable class="w100">
								<el-option label="com.mysql.cj.jdbc.Driver" value="com.mysql.cj.jdbc.Driver"></el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="数据库连接地址" prop="url">
                            <el-input v-model="formData.url" placeholder="请输入数据库连接地址" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据库用户名" prop="username">
                            <el-input v-model="formData.username" placeholder="请输入数据库用户名" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="数据库密码" prop="password">
                            <el-input v-model="formData.password" placeholder="请输入数据库密码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="状态" prop="status">
                            <el-select v-model="formData.status" placeholder="请选择状态" clearable class="w100">
								<el-option label="正常" value="0"></el-option>
								<el-option label="停用" value="1"></el-option>
								<el-option label="删除" value="2"></el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addDatasource } from '/@/api/system/datasource/index';
export default {
    name: 'system/datasource/add',
    setup(props, context) {
        const addDatasourceFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            formData: {
                status: '0',
            },
            formRules : {
                databaseName: { required: true, message: '数据源名称不能为空', trigger: 'blur' },
                databaseType: { required: true, message: '数据库类型不能为空', trigger: 'blur' },
                datasourceType: { required: true, message: '数据源类型不能为空', trigger: 'blur' },
                driverClassName: { required: true, message: '数据库驱动类名称不能为空', trigger: 'blur' },
                url: { required: true, message: '数据库连接地址不能为空', trigger: 'blur' },
                username: { required: true, message: '数据库用户名不能为空', trigger: 'blur' },
            }
        });

        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            addDatasourceFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addDatasourceFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        databaseName: state.formData.databaseName,
                        databaseType: state.formData.databaseType,
                        datasourceType: state.formData.datasourceType,
                        driverClassName: state.formData.driverClassName,
                        url: state.formData.url,
                        username: state.formData.username,
                        password: state.formData.password,
                        status: state.formData.status,
                        remark: state.formData.remark,
                    }
                    addDatasource(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            addDatasourceFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>